﻿using ScanConfirm.Business;
using ScanConfirm.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScanConfirmForm
{
    public partial class FrmCarTypeSet : Form
    {
        private CarTypeBLL productionModelsBLL = new CarTypeBLL();
        CarTypeModel _ProductionModel;
        public Action RefreshUiEvent;
        public FrmCarTypeSet()
        {
            InitializeComponent();
        }

        public FrmCarTypeSet(CarTypeModel carTypeModel)
        {
            InitializeComponent();
            _ProductionModel = carTypeModel;
            txtCarCode.Text = carTypeModel.CarCode;
            txtCarName.Text = carTypeModel.CarName;
            txtTriaName.Text = carTypeModel?.TriaName;
            txtTriaIndex.Text = carTypeModel?.TriaIndex.ToString();
            txtTriaCode.Text = carTypeModel?.TriaCode;
            txtVersion.Text = carTypeModel?.Version;
            btnSave.Text = "修改";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txtTriaName.Text))
            {
                MessageBox.Show("请输入端末号！");
                return;
            }

            if (string.IsNullOrEmpty(txtTriaCode.Text))
            {
                MessageBox.Show("请输入端末标识！");
                return;
            }

            if (string.IsNullOrEmpty(txtTriaIndex.Text))
            {
                MessageBox.Show("请输入端末标识位置！");
                return;
            }
            var triIndex = 0;
            var isint=int.TryParse(txtTriaIndex.Text, out triIndex);
            if (!isint)
            {
                MessageBox.Show("端末标识位置必须是数字");
                return;
            }

            if (string.IsNullOrEmpty(txtCarCode.Text))
            {
                MessageBox.Show("请输入车型代码！");
                return;
            }

            if (string.IsNullOrEmpty(txtVersion.Text))
            {
                MessageBox.Show("请输入配置！");
                return;
            }

            if (_ProductionModel == null)
            {
                var model = productionModelsBLL.GetCarModel(txtCarCode.Text, txtCarName.Text);
                if (model != null)
                {
                    MessageBox.Show("已存在相同的车型配置，不能重复！");
                    return;
                }
                _ProductionModel = new CarTypeModel();
                _ProductionModel.CarName = txtCarName.Text;
                _ProductionModel.CarCode = txtCarCode.Text;
                _ProductionModel.TriaName = txtTriaName.Text;
                _ProductionModel.TriaIndex = triIndex;
                _ProductionModel.SyncFlag = 0;
                _ProductionModel.Version = txtVersion.Text;
                _ProductionModel.TriaCode = txtTriaCode.Text;
                var result = productionModelsBLL.Add(_ProductionModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加车型成功");
                    RefreshUiEvent?.BeginInvoke(null,null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加车型失败：" + result.Dsca);
            }
            else
            {
                _ProductionModel.CarName = txtCarName.Text;
                _ProductionModel.CarCode = txtCarCode.Text;
                _ProductionModel.TriaName = txtTriaName.Text;
                _ProductionModel.TriaIndex = triIndex;
                _ProductionModel.Version = txtVersion.Text;
                _ProductionModel.TriaCode = txtTriaCode.Text;
                var result = productionModelsBLL.Update(_ProductionModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改车型成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改车型失败：" + result.Dsca);
            }
        }

    }
}
